package com.example.studentapp.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentapp.adapters.CourseListNowAdapter;
import com.example.studentapp.beans.Course;
import com.example.studentapp.course.CourseAddActivity;
import com.example.studentapp.R;
import com.example.studentapp.course.LookActivity;
import com.example.studentapp.db.SQLiteDB;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class CourseFragment extends Fragment implements View.OnClickListener {
    TextView course_todayTV,course_addTV,course_editTV,course_warnTV;
    ImageView course_addIV,course_editIV;
    ListView courseLV;
    int week_day_now = 0;
    SQLiteDB sqLiteDB;
    SQLiteDatabase sqLiteDatabase;
    int course_list_id;
    // 课程信息
    List<Course> list;
    CourseListNowAdapter courseListNowAdapter;




    @SuppressLint("Range")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View root = inflater.inflate(R.layout.fragment_course, container, false);
        course_todayTV = root.findViewById(R.id.course_todayTV);
        course_todayTV.setText(getDate());
        course_addTV= root.findViewById(R.id.course_addTV);
        course_addIV= root.findViewById(R.id.course_addIV);
        course_editIV= root.findViewById(R.id.course_editIV);
        course_warnTV= root.findViewById(R.id.course_warnTV);
        course_editTV= root.findViewById(R.id.course_editTV);
        courseLV= root.findViewById(R.id.courseLV);
        course_addTV.setOnClickListener(this);
        course_addIV.setOnClickListener(this);
        course_editTV.setOnClickListener(this);
        course_editIV.setOnClickListener(this);

        // 数据的获取
        sqLiteDB = new SQLiteDB(getActivity());
        sqLiteDatabase = sqLiteDB.getWritableDatabase();
        // 查询课程列表
        String sql = "select * from course_list";
        Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
        if(cursor.getCount() > 0){
            //获取添加后的course_list_id
            String course_sql = "select * from course_list order by id desc";
            Cursor course_cursor = sqLiteDatabase.rawQuery(course_sql, null);
            if(course_cursor.getCount()>0){
                course_cursor.moveToFirst();
                course_list_id = course_cursor.getInt(course_cursor.getColumnIndex("id"));

                list=new ArrayList<>();
                // 课程详情
                String cd_sql = "select * from course_detail where course_list_id=" + course_list_id
                        +" and section like '"+week_day_now+"%' order by section";
                Cursor cd_cursor = sqLiteDatabase.rawQuery(cd_sql, null);
                if(cd_cursor.getCount()>0){
                    cd_cursor.moveToFirst();
                    for(int i =0;i<cd_cursor.getCount();i++){
                        String name = cd_cursor.getString(cd_cursor.getColumnIndex("name"));
                        int id = cd_cursor.getInt(cd_cursor.getColumnIndex("id"));
                        int section = cd_cursor.getInt(cd_cursor.getColumnIndex("section"));
                        String place = cd_cursor.getString(cd_cursor.getColumnIndex("place"));
                        String teacher = cd_cursor.getString(cd_cursor.getColumnIndex("teacher"));
                        list.add(new Course(name, place, teacher, section, id, course_list_id));
                        cd_cursor.moveToNext();
                    }
                    // 添加listVie适配器
                    courseListNowAdapter = new CourseListNowAdapter(getActivity(),list);
                    courseLV.setAdapter(courseListNowAdapter);
                    course_warnTV.setVisibility(View.GONE);
                    courseLV.setVisibility(View.VISIBLE);


                }else{
                    course_warnTV.setVisibility(View.VISIBLE);
                    courseLV.setVisibility(View.GONE);
                    course_warnTV.setText(getString(R.string.course_no_class));
                }
            }else{
                Toast.makeText(getActivity(), getString(R.string.course_id_error), Toast.LENGTH_SHORT).show();
            }
        }else{
            course_warnTV.setVisibility(View.VISIBLE);
            courseLV.setVisibility(View.GONE);
            course_warnTV.setText(getString(R.string.course_no));
        }


        return root;
    }
    // 获取当前时间
    public String getDate(){
        String dateInfo = "";
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int daym = calendar.get(Calendar.DAY_OF_MONTH);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        String month_s = "";
        String daym_s = "";
        month++;
        if(month < 0){
            month_s = "0" + month;
        }else{
            month_s = month+"";
        }
        if(daym<10){
            daym_s = "0" + daym;
        }else{
            daym_s = daym+"";
        }
        dateInfo = getString(R.string.today) + year + getString(R.string.year) + month_s + getString(R.string.month)
                + daym_s + getString(R.string.day) + " " + getWeek(week-1);


        return dateInfo;

    }

    public String getWeek(int week){
        String day = "";
        switch (week){
            case 0:
                // 星期天
                day = getString(R.string.week_seven);
                week_day_now =7;
                break;
            case 1:
                day = getString(R.string.week_one);
                week_day_now = 1;
                break;
            case 2:
                day = getString(R.string.week_two);
                week_day_now = 2;
                break;
            case 3:
                day = getString(R.string.week_three);
                week_day_now = 3;
                break;
            case 4:
                day = getString(R.string.week_four);
                week_day_now = 4;
                break;
            case 5:
                day = getString(R.string.week_five);
                week_day_now = 5;
                break;
            case 6:
                day = getString(R.string.week_six);
                week_day_now = 6;
                break;
        }
        return day;
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()){
            // 添加
            case R.id.course_addIV:
            case R.id.course_addTV:
                intent = new Intent(getActivity(),CourseAddActivity.class);
                startActivity(intent);
                break;
                // 编辑
            case R.id.course_editIV:
            case R.id.course_editTV:
                intent = new Intent(getActivity(), LookActivity.class);
                startActivity(intent);
                break;


        }

    }
}